import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class jdbc {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        int age = scanner.nextInt();
        //1.先创建DataSource
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=latin1&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("root");

        //2.建立和数据库服务器之间的链接，连接好后，才能进行后续
        Connection connection = dataSource.getConnection();

        //3.构建sql
//        String createTable = "create table student(name varchar(10), age int)";
//        String insertData = "insert into student values (\"zhangsan\", 12)";
        String insertData = "insert into student values(?,?)";
//        PreparedStatement statement = connection.prepareStatement(createTable);
//        PreparedStatement statement1 = connection.prepareStatement(insertData);
        PreparedStatement statement = connection.prepareStatement(insertData);
        statement.setString(1,name);
        statement.setInt(2,age);

        //4. 把sql发给服务器, 返回时一个整数, 表示影响到的行数
        int n = statement.executeUpdate();
//        int n1 = statement1.executeUpdate();

        
        statement.close();
        connection.close();
    }
}
